package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.system.domain.BiddingDemand;
import com.ruoyi.system.domain.HomeMiddleData;
import com.ruoyi.system.domain.vo.DealClosedVo;
import com.ruoyi.system.mapper.BiddingDemandMapper;
import com.ruoyi.system.mapper.HomeMiddleDataMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserOrderMapper;
import com.ruoyi.system.service.MiddleShowService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class MiddleShowServiceImpl implements MiddleShowService {

    @Resource
    private HomeMiddleDataMapper homeMiddleDataMapper;

    @Resource
    private BiddingDemandMapper biddingDemandMapper;

    @Resource
    private SysUserOrderMapper sysUserOrderMapper;

    @Resource
    private SysUserMapper sysUserMapper;


    /**
     * @return 返回查询到的数据
     * @Deprecated 查询首页中间数据
     */
    @Override
    public AjaxResult queryMiddleData(LoginUser loginUser) {

        ArrayList<HomeMiddleData> arrayList = homeMiddleDataMapper.queryMiddleShowDataList();
        HashMap<String, Object> hashMap = new HashMap<>();
        ArrayList<HomeMiddleData> programCodeSell = new ArrayList<>();
        ArrayList<HomeMiddleData> programToolsSell = new ArrayList<>();
        ArrayList<HomeMiddleData> accountSell = new ArrayList<>();
        ArrayList<HomeMiddleData> qtSell = new ArrayList<>();
        for (HomeMiddleData homeMiddleData : arrayList) {
            // 如果当前遍历的用户id和首页里面的用户id一样代表是自己发的 那么直接不需要展示
//            if (loginUser.getUserId().equals(homeMiddleData.getUserId())){
//                continue;
//            }
            // 源码
            if (homeMiddleData.getType().equals("0")) {
                programCodeSell.add(homeMiddleData);
            }
            // 工具
            if (homeMiddleData.getType().equals("1")) {
                programToolsSell.add(homeMiddleData);
            }
            // 账号
            if (homeMiddleData.getType().equals("2")) {
                accountSell.add(homeMiddleData);
            }
            // 其它
            if (homeMiddleData.getType().equals("3")) {
                qtSell.add(homeMiddleData);
            }
        }
        hashMap.put("ProgramCodeSell", programCodeSell);
        hashMap.put("programToolsSell", programToolsSell);
        hashMap.put("accountSell", accountSell);
        hashMap.put("qtSell", qtSell);
        return AjaxResult.success("查询成功", hashMap);
    }

    /**
     * @return 返回查询到的数据
     * @Deprecated 查询底部数据
     */
    @Override
    public AjaxResult queryBottomData() {
        HashMap<String, Object> hashMap = new HashMap<>();
        // 封装需求榜
        List<BiddingDemand> biddingDemands = biddingDemandMapper.queryBiddingDemandLeveDataList();
        // 封装成交榜
        List<DealClosedVo> dealClosedVos = sysUserOrderMapper.queryUserOrderLeveDataList();
        // 根据用户id查询用户名称
        for (DealClosedVo dealClosedVo : dealClosedVos) {
            SysUser sysUser = sysUserMapper.selectUserById(Long.valueOf(dealClosedVo.getUserId().toString()));
            if (sysUser != null)
                dealClosedVo.setUserName(sysUser.getUserName());
        }
        // 封装贡献榜
        List<SysUser> sysUsers = sysUserMapper.queryUserLeveDataList();
        hashMap.put("xqb", biddingDemands);
        hashMap.put("cjb", dealClosedVos);
        hashMap.put("gxb", sysUsers);
        return AjaxResult.success("查询成功", hashMap);
    }
}
